VitestでIn-source testing
setup
code:vitest.config.ts
/// <reference types="vitest" />
import { defineConfig } from 'vite'
export default defineConfig({
test: {
},
})
tsconfig.ts
code:ts
{
"compilerOptions": {
"types": [
"vitest/importMeta" ←これ
]
}
}
code: src/index.ts
// the implementation
export function add(...args: number[]) {
return args.reduce((a, b) => a + b, 0)
}
// in-source test suites
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest
it('add', () => {
expect(add()).toBe(0)
expect(add(1)).toBe(1)
expect(add(1, 2, 3)).toBe(6)
})
}
使っているのものによって設定方法が変わるのか #??